package com.ltt.controller;

import com.ltt.common.UserThreadLocal;
import com.ltt.dao.pojo.Harvest;
import com.ltt.dao.vo.HarvestVo;
import com.ltt.dao.vo.Result;
import com.ltt.dao.vo.params.HarvestParams;
import com.ltt.service.HarvestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ‘李天泰’
 * @version 1.0
 * @description: TODO
 * @date 2023/5/2 10:48
 */
@Api(tags = "地址模块")
@RestController
@RequestMapping("/harvest")
public class HarvestController {
    @Autowired
    private HarvestService harvestService;

    @ApiOperation("查找用户的所有地址")
    @PostMapping("/find")
    public Result findAllHarvest() {
        List<HarvestVo> allHarvest = harvestService.findAllHarvest(UserThreadLocal.get().getOpenid());
        return Result.successWithData(allHarvest);
    }

    @ApiOperation("增加地址")
    @PostMapping("/insert")
    public void insertHarvest(@RequestBody HarvestParams harvestParams) {
        Harvest harvest = new Harvest();
        BeanUtils.copyProperties(harvestParams, harvest);
        harvest.setOpenid(UserThreadLocal.get().getOpenid());
        harvestService.insertHarvest(harvest);
    }

    @ApiOperation("更新用户的地址")
    @PostMapping("/update")
    public void updateHarvest(@RequestBody HarvestVo harvestVo) {
        Harvest harvest = new Harvest();
        BeanUtils.copyProperties(harvestVo, harvest);
        harvest.setOpenid(UserThreadLocal.get().getOpenid());
        harvestService.updateHarvest(harvest);
    }

    @ApiOperation("删除用户的地址")
    @PostMapping("/delete")
    public void deleteHarvest(@ApiParam("地址的唯一标识") @RequestParam int id) {
        harvestService.deleteHarvest(id);
    }
}
